package cn.jackyu.stock.stock_grid_trading.event.consumer;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class StockDataConsumerManager {

    private final Map<String, StockDataConsumer> consumerMap = new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        // 获取所有 StockDataConsumer 的实现类 Bean
        Map<String, StockDataConsumer> beans = applicationContext.getBeansOfType(StockDataConsumer.class);
        for (StockDataConsumer consumer : beans.values()) {
            if (consumer instanceof AbstractStockDataConsumer abstractConsumer) {
                String stockCode = abstractConsumer.getStockCode();
                consumerMap.put(stockCode, consumer);
            }
        }
    }

    public StockDataConsumer getConsumer(String code) {
        return consumerMap.get(code);
    }
}